//
//  YKFarmListController.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/11.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmListController.h"
#import "YKFarmDataListController.h"
#import "YKFarmFarmAddController.h"
#import "YKFarmViewModel.h"
#import <YK_TableRefresh/YK_TableRefresh.h>
#import <YKAlert/YKAlertCenter.h>
#import <Masonry/Masonry.h>
#import <YK_ExectionModel/YKNSObjectCore.h>
#import <YK_ExectionModel/YKUIKitCore.h>


@interface YKFarmListController ()<UITableViewDelegate,UITableViewDataSource>
///
@property(nonatomic,strong) YKFarmViewModel *viewModel;
///
@property(nonatomic,copy) NSString *userId;
///
@property(nonatomic,strong) UITableView *tableView;
@end

@implementation YKFarmListController

- (instancetype)initWithUserId:(NSString *)userId
{
    self = [super init];
    if (self) {
        self.title = @"农场";
        self.userId = userId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.3
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [btn setTitle:@"添加记录" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn sizeToFit];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = item;
    
    @weakify(self);
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        YKFarmFarmAddController *vc = [[YKFarmFarmAddController alloc] initWithBlock:^(NSString * _Nonnull name) {
            [self.viewModel.addfarmCommand execute:@{@"userId":self.userId,@"name":name}];
        }];
        [self presentViewController:vc animated:YES completion:nil];
    }];
    [self.view addSubview:self.tableView];
    [self.viewModel.farmListCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    [[self.viewModel.farmListCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            [YKAlertCenter showLoading:@"正在加载"];
        }else
        {
            [YKAlertCenter dissLoading];
        }
    }];
    [self.viewModel.addfarmCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"创建成功"];
        [self.viewModel.farmListCommand execute:nil];
    }];
    [self.viewModel.farmListCommand execute:nil];
}
- (YKFarmViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YKFarmViewModel alloc]initWithUserId:self.userId];
        @weakify(self);
        [_viewModel.errorSubject subscribeNext:^(NSError * _Nullable x) {
            @strongify(self);
            [YKAlertCenter showMessage:x.localizedDescription];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }];
    }
    return _viewModel;
}

#pragma mark -tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.farmListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class)];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass(UITableViewCell.class)];
        cell.backgroundColor = UIColor.clearColor;
        cell.contentView.backgroundColor = UIColor.clearColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    @weakify(cell);
    [self.viewModel getFarmListName:indexPath block:^(NSString * _Nonnull name, NSString * _Nonnull farmListId) {
        @strongify(cell);
        cell.textLabel.text = name;
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    [self.viewModel getFarmListName:indexPath block:^(NSString * _Nonnull name, NSString * _Nonnull farmListId) {
        @strongify(self);
        YKFarmDataListController *vc = [[YKFarmDataListController alloc] initWithDataId:farmListId viewModel:self.viewModel];
        vc.title = name;
        [self.navigationController pushViewController:vc animated:YES];
    }];
}
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 85;
        tableView.rowHeight = 44;
        tableView.backgroundColor = UIColor.clearColor;
        @weakify(self);
        tableView.mj_header = [YKRefreshHeader headerNormalWithViewModel:self.viewModel dataSource:self.viewModel.farmListArray refreshingBlock:^{
            @strongify(self);
            //刷新动作
            [self.viewModel.farmListCommand execute:nil];
        }];
        tableView.mj_footer = [YKRefreshFooter footerWithViewModel:self.viewModel refreshingBlock:^{
            @strongify(self);
            [self.viewModel.farmListCommand execute:nil];
        }];
        [tableView registerClass:UITableViewCell.class forCellReuseIdentifier:UITableViewCell.defaultIdentifier];
        _tableView = tableView;
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
